package no.ntnu.fp.model;

import java.sql.ResultSet;
import java.util.ArrayList;

import no.ntnu.fp.storage.DBConnection;

public class Appointment extends ActiveRecord {
	private String date;
	private String start;
	private String stop;
	private String description;
	private String place;
	private int numberOfParticipants;
	private String username;
	private int aId;
	
	public Appointment(String avtaleDato, String startTid, String sluttTid,
			String avtaleBeskrivelse, String avtaleSted, int numberOfParticipants){
		this.date = avtaleDato;
		this.start = startTid;
		this.stop = sluttTid;
		this.description = avtaleBeskrivelse;
		this.place = avtaleSted;
		this.numberOfParticipants = numberOfParticipants;
	}
	
	public Appointment(int aId) {
		this.aId = aId;
		db = new DBConnection();
		String query = "select Sted, Dato, Starttidspunkt, Sluttidspunkt, AntallDeltakere, Beskrivelse" +
				" from Avtale where AvtaleID = " + aId;
		ResultSet rs = db.makeSingleQuery(query);
		
		while (next(rs)) {
			this.place = getString(rs, "Sted");
			this.date = getString(rs, "Dato");
			this.start = getString(rs, "Starttidspunkt");
			this.stop = getString(rs, "Sluttidspunkt");
			this.numberOfParticipants = getInt(rs, "AntallDeltakere");
			this.description = getString(rs, "Beskrivelse");
		}
		closeDBConnection();
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Override
	public String toString() {
		return "Dette er avtalen din:\n" +
				"Avtale dato: " + this.date + " Avtalen starter: " + this.start + " og blir avsluttet: " + this.stop + "\n" +
				"Den blir holdt pa/i: " + this.place + " og " + this.numberOfParticipants + " personer er invitert. \n" +
				"Beskrivelsen av avtalen er: " + this.description;
	}
	
	@Override
	public void save() {
		db=new DBConnection();
		
		String query="insert ignore into Avtale(sted,dato,starttidspunkt,sluttidspunkt,antalldeltakere," +
				"beskrivelse) values (?,?,?,?,?,?)";
		setPreparedStatement(query);
		setString(1, this.place);
		setString(2, this.date);
		setString(3, this.start);
		setString(4, this.stop);
		setInt(5, this.numberOfParticipants);
		setString(6, this.description);
		addExecuteAndClosePS();
		
		//ma oppdatere oppretteravtale-tabellen:
		String queryAvtaleID = "select AvtaleID " +
				"from Avtale " +
				"where Sted = " + "\""+ place +"\"" + " " +
				"and Dato = " + "\""+ date +"\"" + " " +
				"and Starttidspunkt = " + "\""+ start +"\"" + " " +
				"and Sluttidspunkt = " + "\""+ stop +"\"" + " " +
				"and AntallDeltakere = " + numberOfParticipants + " " +
				"and Beskrivelse = " + "\""+ description +"\"";
		
		ResultSet rs = db.makeSingleQuery(queryAvtaleID);
		int avtaleID = 0;
		if (next(rs)) {
			avtaleID = getInt(rs, "AvtaleID");
		}
		else
			System.out.println("ingen resultater");
		
		String query2 = "insert ignore into OppretterAvtale(Brukernavn, AvtaleID) values(?,?)";
		setPreparedStatement(query2);
		
		setString(1, username);
		setInt(2, avtaleID);
		
		addExecuteAndClosePS();
		closeDBConnection();
	}

	@Override
	public void edit(int index) {
		db = new DBConnection();
		String query = "update Avtale set ";
		
		String[] columns = {"Sted", "Dato", "Starttidspunkt", "Sluttidspunkt", "AntallDeltakere", "Beskrivelse"};
		Object[] fields = {place, date, start, stop, numberOfParticipants, description};
		
		query += "" + columns[index] + " = \"" + fields[index] + "\" where AvtaleID = " + aId;
		
		db.makeUpdate(query);
		closeDBConnection();
	}

	@Override
	public void delete() {
		System.out.println("Appointment model - delete: " + this.username);

		db = new DBConnection();
		String query1 = "select InnkallingsID from Moteinnkalling " +
				"where AvtaleID = " + aId;
		ResultSet rs = db.makeSingleQuery(query1);
		if (next(rs)) {
			String query3 = "update Moteinnkalling " +
					"set Status = "+ "\"" + "Avslatt" + "\"" + " " +
					"where AvtaleID = " + aId + " " +
					"and Deltaker = " + "\"" + this.username + "\"";
			db.makeUpdate(query3);
		}
		else {
			String query2 = "DELETE FROM Avtale where AvtaleID = " + aId;
			db.makeUpdate(query2);
		}
		
		closeDBConnection(db);
	}
	
	public static ArrayList<Appointment> getDeleteable(String username) {
		DBConnection db = new DBConnection();

		ArrayList<Appointment> deleteable = new ArrayList<Appointment>();

		String query1 = "SELECT Avtale.AvtaleID, Beskrivelse " +
				"FROM Avtale, OppretterAvtale " +
				"WHERE Avtale.AvtaleID = OppretterAvtale.AvtaleID " +
				"AND Brukernavn = " + "\"" + username + "\"";
		String query2 = "SELECT Avtale.AvtaleID, Beskrivelse " +
				"FROM Avtale, Moteinnkalling " +
				"WHERE Avtale.AvtaleID = Moteinnkalling.AvtaleID " +
				"AND Deltaker = " + "\"" + username + "\"";

		ResultSet rs1 = db.makeSingleQuery(query1);
		ResultSet rs2 = db.makeSingleQuery(query2);
		
		Appointment appointment;
		while (next(rs1)) {
			int aId1 = getInt(rs1, "AvtaleID");
			appointment = new Appointment(aId1);
			appointment.setUsername(username);
			String description1 = getString(rs1, "Beskrivelse");
			appointment.setDescription(description1);
			
			deleteable.add(appointment);
		}
		
		while (next(rs2)) {
			int aId2 = getInt(rs2, "AvtaleID");
			appointment = new Appointment(aId2);
			appointment.setUsername(username);
			String description2 = getString(rs2, "Beskrivelse");
			appointment.setDescription(description2);
			
			deleteable.add(appointment);
		}
		
		closeDBConnection(db);
		
		return deleteable;
	}

	public String getDate() {
		return date;
	}

	public String getStart() {
		return start;
	}

	public String getStop() {
		return stop;
	}

	public String getDescription() {
		return description;
	}

	public String getPlace() {
		return place;
	}

	public int getNumberOfParticipants() {
		return numberOfParticipants;
	}
	
	public int getAId() {
		return this.aId;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public void setStop(String stop) {
		this.stop = stop;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public void setNumberOfParticipants(int numberOfParticipants) {
		this.numberOfParticipants = numberOfParticipants;
	}
}
